<?php

class Firm extends Controller {

    public function __construct() {
        parent::Controller();
        if (!$this->session->userdata('isAuthenticated')) {
            redirect(base_url().'/admin.php?c=auth&m=index');
        }
    }

    public function index() {
        if ($firm = $this->getFirm()) {
            $data['title'] = '律所简介';
            $data['firm'] = $firm;
            $this->load->view('firm/index', $data);
        } else {
            redirect(base_url().'/admin.php?c=firm&m=create');
        }
    }

    public function create() {
        if ($this->getFirm()) {
            redirect(base_url().'/admin.php?c=firm&m=edit');
        } else {
            $this->load->helper('form');
            $data['title'] = '律所简介';
            $this->load->view('firm/create', $data);
        }
    }

    public function add() {
        if ($this->getFirm()) {
            redirect(base_url().'/admin.php?c=firm&m=edit');
            exit;
        }
        $this->load->library('form_validation');
		
		$this->form_validation->set_rules('content', '律所简介', 'required');
		
        if ($this->form_validation->run() == false) {
            $this->load->view('firm/create');
        } else {
            $now = date('Y-m-d H:i:s', time());
            $firm['content'] = $this->input->post('content');
            $firm['created_at'] = $photo['updated_at'] = $now;
            if ($this->db->insert('firm', $firm)) {
                $this->session->set_flashdata('notice', '数据新增成功！');
                redirect(base_url().'/admin.php?c=firm&m=index');
            } else {
                $this->session->set_flashdata('error', '数据库错误！');
                redirect(base_url().'/admin.php?c=firm&m=create');
            }
        }
    }

    public function edit() {
        if ($firm = $this->getFirm()) {
            $this->load->helper('form');
            $data['firm'] = $firm;
            $this->load->view('firm/edit', $data);
        } else {
            redirect(base_url().'/admin.php?c=firm&m=create');
        }
    }

    public function update() {
        if ($firm = $this->getFirm()) {
            $this->load->library('form_validation');
			
			$this->form_validation->set_rules('content', '律所简介', 'required');
			
            if ($this->form_validation->run() == false) {
                $this->load->helper('form');
                $data['firm'] = $firm;
                $this->load->view('firm/edit', $data);
            } else {
                $f['content'] = $this->input->post('content');
                $f['updated_at'] = date('Y-m-d H:i:s', time());
                if ($this->db->update('firm', $f, array('id' => 1))) {
                    $this->session->set_flashdata('notice', '数据更新成功！');
                } else {
                    $this->session->set_flashdata('error', '更新失败，数据库错误！');
                }
                redirect(base_url().'/admin.php?c=firm&m=index');
            }
        } else {
            redirect(base_url().'/admin.php?c=firm&m=create');
        }
    }

    private function getFirm() {
        $query = $this->db->query("select * from firm where id=1 limit 1");
        return ($query->num_rows() > 0) ? $query->row() : false;
    }

}